/************************************* * File: Money.cpp * Author: Katherine Gibson * Date: 3/1/2016 * Section: N/A * E-mail: k38@umbc.edu * Description: * This file contains the full * definition for a Money class * including the overloaded subtract *************************************/ #include "Money.h" #include #include using namespace std; Money::Money() { // left blank } Money::Money(int dollars, int cents) { m_dollars = dollars; m_cents = cents; } void Money::Output(string name) { cout << name << " is $" << m_dollars << "." << m_cents << endl; } Money Money::operator- ( const Money& amount2) { int dollarsRet, centsRet; // if we had money1 - money2 // "this" is money1, and "amount2" is money2 // we actually don't need to use "this" in this function, though // if the subtracted cents are larger, we prevent // them from going negative if (m_cents <= amount2.m_cents) { dollarsRet = m_dollars - amount2.m_dollars - 1; centsRet = 100 + (m_cents - amount2.m_cents); } // otherwise, proceed as normal else { centsRet = m_cents - amount2.m_cents; dollarsRet = m_dollars - amount2.m_dollars; } // return a new money object return Money(dollarsRet, centsRet); }